package com.my.yyr.config;

import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.session.FlushMode;
import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession;
import org.springframework.session.web.http.CookieSerializer;
import org.springframework.session.web.http.DefaultCookieSerializer;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.nio.charset.StandardCharsets;
import java.util.List;

@EnableRedisHttpSession(flushMode = FlushMode.IMMEDIATE)
@MapperScan("com.my.yyr.dao")//扫描映射器
public class CommonsConfig {

    @Bean
    public CookieSerializer cookieSerializer() {
        DefaultCookieSerializer cookieSerializer = new DefaultCookieSerializer();
        cookieSerializer.setSameSite(null);
        return cookieSerializer;
    }

    @Bean
    public WebMvcConfigurer webMvcConfigurer(){

        return new WebMvcConfigurer() {
            /*
            //设置允许接受跨域请求
            @Override
            public void addCorsMappings(CorsRegistry registry) {

                registry.addMapping("/**")//允许对任何路径进行跨域请求
                        .allowedMethods("*")//允许任何跨域请求方式
                        .allowCredentials(true);//允许跨域请求使用session

            }

            @Override
            public void addViewControllers(ViewControllerRegistry registry) {
                registry.addRedirectViewController("/","/index.html");
            }
            */

            @Override
            public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
                StringHttpMessageConverter stringHttpMessageConverter = new StringHttpMessageConverter();
                stringHttpMessageConverter.setDefaultCharset(StandardCharsets.UTF_8);
                converters.add(0,stringHttpMessageConverter);

                FastJsonHttpMessageConverter fastJsonHttpMessageConverter = new FastJsonHttpMessageConverter();
                fastJsonHttpMessageConverter.setDefaultCharset(StandardCharsets.UTF_8);
                converters.add(1,fastJsonHttpMessageConverter);
            }
        };

    }

}
